﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ObserverPattern
{
    public class Portfolio : IObserver
    {
        public string Name { get; set; }
        public uint Value { get; private set; }
        public Dictionary<Stock, uint> Stocks { get; private set; }
        private PortfolioDisplay _portfolioDisplay = new PortfolioDisplay();
        
        public Portfolio()
        {
            Stocks = new Dictionary<Stock, uint>();
        }

        public void AddStock(Stock stock)
        {
            stock.Attach(this);
            uint value;
            if (Stocks.TryGetValue(stock, out value))
            {
                Stocks[stock] = ++value;
                //System.Console.WriteLine("Existing stock at count: " + value );
            }
            else
            {
                Stocks.Add(stock,1);
                //System.Console.WriteLine("New stock at count: " + Stocks[stock].ToString()) ;
            }

            //System.Console.WriteLine("Name of stock: " + stock.Name);
        }

        public void RemoveStock (Stock stock)
        {
            stock.Detach(this);
        }

        public void Update()
        {
            
        }


        public void Update(Stock stock)
        {
            _portfolioDisplay.Print(this);
        }
    }
}
